package com.panda.order.taobao;


import com.panda.order.domain.TPandaOrderHead;
import com.panda.system.common.constant.EnumConstants;
import com.panda.system.common.constant.RedisKeyConstants;
import com.panda.system.domain.PandaStore;
import com.panda.system.service.IPandaStoreService;
import com.panda.system.service.ISysConfigService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TradeMemoUpdateRequest;
import com.taobao.api.response.TradeMemoUpdateResponse;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TbUpdateOrderFlgApi {

    private static final Logger log = LoggerFactory.getLogger(TbUpdateOrderFlgApi.class);
    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IPandaStoreService storeService;


    public void updateOrderFlg(List<TPandaOrderHead> orderHeads,Long flg){
        if(CollectionUtils.isEmpty(orderHeads)){
            log.info("更新淘宝订单旗帜，待更新订单为空！");
            return;
        }
        Map<String, List<TPandaOrderHead>> ordersGroupMap = orderHeads.stream().collect(Collectors.groupingBy(TPandaOrderHead::getStoreId));

        String serverUrl = sysConfigService.selectConfigByKey(RedisKeyConstants.TAOBAO_URL);
        String appKey = sysConfigService.selectConfigByKey(RedisKeyConstants.TAOBAO_APPKEY); // 可替换为您的应用的AppKey
        String appSecret = sysConfigService.selectConfigByKey(RedisKeyConstants.TAOBAO_APPSECRET);
        PandaStore pandaStore = new PandaStore();
        pandaStore.setStatus(EnumConstants.SysStatus.OPEN.getCode());
        List<PandaStore> pandaStores = storeService.selectPandaStoreList(pandaStore);

        TaobaoClient client = new DefaultTaobaoClient(serverUrl, appKey, appSecret);
        for(PandaStore store:pandaStores){
            List<TPandaOrderHead> orderHeadList = ordersGroupMap.get(String.valueOf(store.getStoreId()));
            String sessionKey = store.getSecret();
            if(!CollectionUtils.isEmpty(orderHeadList)&&Strings.isNotEmpty(sessionKey)&&store.getFailureTime()!=null&&new Date().before(store.getFailureTime())){
                TradeMemoUpdateRequest req = new TradeMemoUpdateRequest();
                for(TPandaOrderHead order:orderHeadList){
                    log.info("更新淘宝订单旗帜，开始！订单号：{}",order.getTid());
                    req.setTid(Long.valueOf(order.getTid()));
                    req.setFlag(flg);
                    try {
                        TradeMemoUpdateResponse rsp = client.execute(req, sessionKey);
                        if(rsp.getCode() == "50"){
                            log.info("更新淘宝订单旗帜，更新失败！");
                        }
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                }


            }
        }
    }
}
